package com.yzx.lifeassistants.model.analysis;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.yzx.lifeassistants.bean.OfficeInform;
import com.yzx.lifeassistants.utils.LogcatUtils;

/**
 * @Description: 解析查询教务处通知返回的结果
 * @author: yzx
 * @time: 2016-3-10 下午9:57:35
 */
public class QueryOfficeInformAnalysis {
	/**
	 * 
	 * @Description: 是否暂无更多
	 */
	public static Boolean isNoMore(String response) {
		Boolean isNoMore = true;
		try {
			JSONObject jsonObject = new JSONObject(response);
			if (jsonObject.getBoolean("status")) {
				isNoMore = false;
			} else {
				String errMsg = jsonObject.getString("errMsg");
				LogcatUtils.e("解析查询教务处通知返回的结果失败", errMsg);
			}
		} catch (JSONException e) {
			if (null != e.getMessage()) {
				LogcatUtils.e("解析查询教务处通知返回的结果异常", e.getMessage());
			}
		}
		return isNoMore;
	}

	/**
	 * 
	 * @Description: 解析返回的图书列表
	 */
	public static List<OfficeInform> getData(String response) {
		List<OfficeInform> officeInforms = new ArrayList<OfficeInform>();
		try {
			JSONObject jsonObject = new JSONObject(response);
			JSONArray array = jsonObject.getJSONArray("data");
			for (int i = 0; i < array.length(); i++) {
				JSONObject object = array.getJSONObject(i);
				OfficeInform inform = new OfficeInform();
				inform.setTitle(object.getString("title"));
				String date = object.getString("date");
				inform.setDate(date.replace("年", "/").replace("月", "/")
						.replace("日", ""));
				inform.setUrl(object.getString("url"));
				if ("true".equals(object.getString("isRed"))) {
					inform.setIsRed(true);
				} else {
					inform.setIsRed(false);
				}
				officeInforms.add(inform);
			}
		} catch (JSONException e) {
			if (null != e.getMessage()) {
				LogcatUtils.e("解析查询教务处通知返回的结果异常", e.getMessage());
			}
		}
		return officeInforms;
	}
}
